﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NPMonitor.DataModel;
using NPMonitor.Helpers;
using NPMonitor.Implementation.Store;
using NPMonitor.Interfaces;
using NPMonitor.Configuration;

namespace NPMonitor.Library.Tests.Store
{
    [TestClass]
    public class EFJobStoreTest
    {
        JobStore jobStore = null;

        [TestInitialize]
        public void Setup()
        {
            jobStore = new JobStore();
        }

        [TestMethod]
        public void ConnectionStringTest()
        {
            IEFJobStoreSettingsProvider settingProvider =
            (IEFJobStoreSettingsProvider)Utils.CreateInstanceWithRequiredInterface(ConfigurationSettings.SettingsProvider.JobStoreSettingsProviderType.AssemblyQualifiedName, "IEFJobStoreSettingsProvider");

            Assert.IsNotNull(settingProvider.ConnectionString);
        }

        [TestMethod]
        public void JobOperationsTest()
        {
            JobData jobData = new JobData()
            {
                Status = JobStatus.Pending,
                UniqueId = Guid.NewGuid(),
                Application = "EFMonitor",
                Name = "SmsTestByYY",
                Description = "CreateJobTest",
                //FileUniqueId = new Guid("6cdc822e-60c8-4dcc-b738-aa111d49bf13"),
                QueueId = 0,
                Instance = "RAY-PC",
                ScheduleType = "NPMonitor.DataModel.CalendarSchedule, NPMonitor.Library",
                Schedule = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
+ "<CalendarSchedule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
  + "<DaysOfWeek />"
  + "<EndDateTime xsi:nil=\"true\" />"
  + "<StartDateTime>2012-12-05T14:03:51</StartDateTime>"
  + "<DailyStartTime>"
    + "<Hour>0</Hour>"
    + "<Minute>0</Minute>"
    + "<Second>0</Second>"
  + "</DailyStartTime>"
+ "</CalendarSchedule>",
                SuppressHistory = false,
                DeleteWhenDone = false,
                MachineName="haifeng-pc"
            };

            long jobId = jobData.Id;

            jobData.NextStartTime = jobData.JobSchedule.GetNextOccurrence();
            jobStore.CreateJob(jobData, null);

            Assert.IsTrue(jobData.Id != jobId);

            jobStore.DeleteJob(jobData.Id);

            jobId = jobData.Id;

            jobStore.CreateJob(jobData, null);

            Assert.IsTrue(jobData.Id != jobId);

            JobExecutionHistory jobHistory = jobStore.CreateJobExecutionHistory(jobData,Guid.NewGuid());

            Assert.IsTrue(jobHistory.Id != 0, "Job history Id shouldn't be 0");

            jobData.Application = "EFMonitor2";
            jobStore.UpdateJob(jobData);

            jobData = jobStore.GetJobs(
                jobIds: new long[] { jobData.Id },
                jobUniqueIds: new Guid[] { jobData.UniqueId },
                applications: new string[] { jobData.Application }
            ).FirstOrDefault();

            Assert.IsNotNull(jobData);
            Assert.AreEqual(jobData.Application, "EFMonitor2");

            jobHistory = jobStore.CreateJobExecutionHistory(jobData,Guid.NewGuid());

            Alert alert = jobStore.CreateAlert(jobData.Id, jobHistory.Id, "EFMonitor2 Test");

            jobStore.SetJobStatuses(new long[] { jobData.Id }, JobStatus.Pending, JobStatus.Scheduled);

            jobData = jobStore.GetScheduleReadyJobs("Ray-PC", DateTime.MaxValue).FirstOrDefault();

            Assert.IsNotNull(jobData);

            jobStore.DeleteJob(jobData, true);

            jobData = jobStore.GetJobs(jobIds: new long[] { jobData.Id }).FirstOrDefault();

            Assert.IsNull(jobData);

        }
    }
}
